package com.yisu.expressway.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;


import com.yisu.expressway.application.ExApplication;

import org.apache.http.util.EncodingUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FileUtil
{
	public static void saveFile(String fileName, String content, boolean deleteFile)
	{
		String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/HuabanLog/";
		File fpath = new File(path);

		if (!fpath.exists())
		{
			fpath.mkdirs();
		}

		File f = new File(path + fileName + ".txt");
		Date date = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = sdf.format(date);
		if (f.exists() && deleteFile)
		{
			f.delete();
			f = new File(path + fileName + ".txt");
		}
		try
		{
			FileWriter fw = new FileWriter(f, !deleteFile);
			fw.write(time + "  \t" + content + "\n");
			fw.close();
		}
		catch (Exception e)
		{
		}
	}


	public static void deleteAll(File file)
	{
		if (null == file)
		{
			return;
		}
		if (file.isFile() || null != file.list() && file.list().length == 0)
		{
			file.delete();
		}
		else
		{
			File[] files = file.listFiles();

			if (null == files)
			{
				return;
			}
			for (File file1 : files)
			{
				deleteAll(file1);
			}

			file.delete();
		}
	}

	/**
	 * Get file content.
	 * @param name
	 * @return String
	 */
	public static String readAssetsFile(String name)
	{
		String res = "";
		try
		{
			InputStream in = ExApplication.getInstance().getResources().getAssets().open(name);

			int length = in.available();
			byte[] buffer = new byte[length];

			in.read(buffer);
			in.close();
			res = EncodingUtils.getString(buffer, "UTF-8");
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * Copy file from oldPath to newPath
	 * @param oldPath
	 * @param newPath
	 */
	public static void copyFile(String oldPath, String newPath) throws IOException {
		int byteread = 0;
		File oldfile = new File(oldPath);
		File newfile = new File(newPath);
		if (newfile.exists())
		{
			newfile.delete();
		}
		newfile.createNewFile();
		if (oldfile.exists())
		{
			FileInputStream inStream = new FileInputStream(oldPath);
			FileOutputStream outStream = new FileOutputStream(newPath);
			byte[] buffer = new byte[1024];
			while ((byteread = inStream.read(buffer)) > 0)
			{
				outStream.write(buffer, 0, byteread);
			}
			inStream.close();
			outStream.close();
		}
	}

	/**
	 * Copy bitmap from oldPath to newPath
	 * @param oldPath
	 * @param newPath
	 */
	public static void copyJPGBitmap(String oldPath, String newPath)
	{
		try
		{
			InputStream is = ExApplication.getInstance().getResources().getAssets().open(oldPath);
			Bitmap image = BitmapFactory.decodeStream(is);
			FileOutputStream out = new FileOutputStream(newPath);
			image.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.flush();
			out.close();
			is.close();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Write data to file.
	 * @param fileName
	 * @param message
	 */
	public static void write2File(String fileName, String message)
	{
		try
		{
			FileOutputStream outStream = new FileOutputStream(fileName);
			byte[] bytes = message.getBytes();
			outStream.write(bytes);
			outStream.close();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	/**
	 * Read data from file.
	 * @param fileName
	 * @return
	 */
	public static String readFile(String fileName)
	{
		String ret = "";
		try
		{
			FileInputStream fin = new FileInputStream(fileName);
			int length = fin.available();
			byte[] buffer = new byte[length];
			fin.read(buffer);
			ret = EncodingUtils.getString(buffer, "UTF-8");
			fin.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		return ret;
	}

	/**
	 * Delete file if exists.
	 * @param path
	 */
	public static void deleteIfExists(String path)
	{
		File f = new File(path);
		if (f.exists())
		{
			f.delete();
		}
	}

	/**
	 * If exists.
	 * @param path
	 * @return
	 */
	public static boolean isFileExists(String path)
	{
		File f = new File(path);
		if (f.exists())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * save Pupai video file path
	 */
	public static  String DIR_NAME = "mocon_video";
	public static  String DIR_NAME_THUM = "mocon_video_thum";

	/**
	 * named file of date format
	 */
	private static  SimpleDateFormat OUTGOING_DATE_FORMAT =
			new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss-SSS");

	/**
	 * get Qupai saved video file path
	 * @return
	 */
	public static File getDoneVideoPath() {
		File dir = new File(Environment.getExternalStorageDirectory()
				+ File.separator + DIR_NAME);

		if (!dir.exists()) {
			dir.mkdirs();
		}

		return dir;
	}

	/**
	 * get Qupai saved video file path
	 * @return
	 */
	public static File getDoneThumPath() {
		File dir = new File(Environment.getExternalStorageDirectory()
				+ File.separator + DIR_NAME_THUM);

		if (!dir.exists()) {
			dir.mkdirs();
		}

		return dir;
	}

	/**
	 * get out put video file path
	 * @return
	 */
	public static String newOutgoingFilePath() {
		String str = OUTGOING_DATE_FORMAT.format(new Date());
		String fileName = getDoneVideoPath() + File.separator
				+ "video_" + str + ".mp4";
		return fileName;
	}

	public static String newOutgoingFilePathForImage() {
		String str = OUTGOING_DATE_FORMAT.format(new Date());
		String fileName = getDoneThumPath() + File.separator
				+ "image_" + str + ".jpg";
		return fileName;
	}
}
